package com.nf.health.app.chatservice.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.nf.health.app.Constants;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.customview.CircleImageView;
import com.nf.health.app.http.Act;
import com.nf.health.app.inquiry.bean.DoctorDetailBean;
import com.nf.health.app.inquiry.docdetail.DocDetailActivity;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.ImageLoaderUtils;
import com.nf.health.app.utils.ViewUtil;

import org.json.JSONArray;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 医生服务评价,与常鹏的assessactvity一样
 *
 * @author hepan
 */
public class EvaluateDoctorActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.iv_head_icon)
    CircleImageView ivHeadIcon;
    @Bind(R.id.doctor_name)
    TextView doctorName;
    @Bind(R.id.doctor_job)
    TextView doctorJob;
    @Bind(R.id.doctor_good)
    TextView doctorGood;
    @Bind(R.id.ll_view)
    LinearLayout llView;
    @Bind(R.id.cb1)
    CheckBox cb1;
    @Bind(R.id.cb2)
    CheckBox cb2;
    @Bind(R.id.cb3)
    CheckBox cb3;
    @Bind(R.id.cb4)
    CheckBox cb4;
    @Bind(R.id.cb5)
    CheckBox cb5;
    @Bind(R.id.et_input_assess)
    EditText etInputAssess;
    @Bind(R.id.bt_assess_commit)
    Button btAssessCommit;
    @Bind(R.id.doctor_hospital)
    TextView doctorHospital;
    @Bind(R.id.rb_satisfied)
    RadioButton rbSatisfied;
    @Bind(R.id.rb_general)
    RadioButton rbGeneral;
    @Bind(R.id.rb_dissatisfied)
    RadioButton rbDissatisfied;
    @Bind(R.id.tv_num_limit)
    TextView tvNumLimit;
    private String doctorId, serviceId, isEnd;
    private DoctorDetailBean doctorDetail;
    private String[] optionsSatisfied = {"回复及时", "态度好", "意见有帮助", "敬业", "专业认真"};
    private String[] optionsGeneral = {"希望更透彻", "希望更热情", "希望更细致"};
    private String[] optionsDissatisfied = {"听不懂", "不敬业", "没有帮助", "不细致", "不友好"};
    private String score = "3";
    public static final String SERVICE_ID = "service_id";
    public static final String IS_END = "is_end";

    private final int MAX_NUM = 150;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_evaluate_doctor);

        doctorId = getIntent().getStringExtra(Constants.DOCTOR_ID);
        serviceId = getIntent().getStringExtra(SERVICE_ID);
        //	1 可以再评价，2 评价结束不可评价 3 健康助手评价
        isEnd = getIntent().getStringExtra(IS_END);
        //请求医生基本信息
        provider.requestDoctorDetail2(doctorId);

        ButterKnife.bind(this);
        initView();
        initListener();
    }

    private void initView() {
        ViewUtil.setText(this, R.id.titlebar_title_tv, "评价");
        etInputAssess.setFilters(new InputFilter[]{new InputFilter.LengthFilter(MAX_NUM)});
    }

    private void initListener() {
        rbSatisfied.setOnClickListener(this);
        rbGeneral.setOnClickListener(this);
        rbDissatisfied.setOnClickListener(this);
        btAssessCommit.setOnClickListener(this);
        etInputAssess.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                tvNumLimit.setText(s.length() + "/" + MAX_NUM);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    /**
     * 提交数据
     */
    private void commitData() {
        String message = etInputAssess.getText().toString().trim();
        ArrayList<String> labelList = getLabel(cb1, cb2, cb3, cb4, cb5);
        String label = new JSONArray(labelList).toString();
        if (labelList.size() == 0) {
            showToast("还未选择评价标签");
            return;
        }
//        if (!"".equals(message) && message.length() < 3) {
//            showToast("评论不能小于3个字哦");
//            return;
//        }
        if (cb1.isChecked() || cb2.isChecked() || cb3.isChecked() || cb4.isChecked() || cb5.isChecked()) {
            if ("3".equals(isEnd)) {
                //健康助手的评价,没有服务id,后台提供了两个接口
                provider.commitDoctorEvaluation(doctorId, score, message, label);
            } else {
                provider.appraiseServiceDoctor(doctorId, isEnd, label, message, score, serviceId);
            }
        } else {
            showToast("请进行评价");
            return;
        }

    }

    private ArrayList<String> getLabel(CheckBox... cks) {
        ArrayList<String> labelList = new ArrayList<>();
        for (int i = 0; i < cks.length; i++) {
            if (cks[i].isChecked()) {
                labelList.add(cks[i].getText().toString());
            }
        }
        return labelList;
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        if (Act.Flag.DOC_DETAIL.equals(actionName)) {
            doctorDetail = (DoctorDetailBean) object;
            ImageLoaderUtils.getinstance(EvaluateDoctorActivity.this).getImage(
                    ivHeadIcon, doctorDetail.getDoctorhimg(),
                    R.drawable.moren_doctor_image);
            doctorName.setText(TextUtils.isEmpty(doctorDetail.getDoctorname()) ? "未填写" : doctorDetail.getDoctorname());
            doctorJob.setText(TextUtils.isEmpty(doctorDetail.getDepartment()) ? "未填写" : doctorDetail.getDepartment());
            doctorHospital.setText(TextUtils.isEmpty(doctorDetail.getHospitalname()) ? "未填写" : doctorDetail.getHospitalname());
            doctorGood.setText(TextUtils.isEmpty(doctorDetail.getProfessional()) ? "未填写" : doctorDetail.getProfessional());
        } else if (Act.Flag.APPRAISE_SERVICE_DOCTOR.equals(actionName) || Act.Flag.APPRAISE_DOCTOR.equals(actionName)) {
            //评论成功,跳到详情
            showToast("评论成功");
            Bundle docDetailBundle = new Bundle();
            docDetailBundle.putString(DocDetailActivity.DOCTORID, doctorId);
            ActivityUtils.showActivity(this, DocDetailActivity.class, docDetailBundle);
            finish();
        }
    }


    private String getAssessSpecific(CheckBox cb) {
        if (cb.isChecked()) {
            return cb.getText().toString().trim();
        }
        return "";
    }

    private String getAssessTitle() {
        if (rbSatisfied.isChecked()) {
            return rbSatisfied.getText().toString();
        }
        if (rbGeneral.isChecked()) {
            return rbGeneral.getText().toString();
        }
        if (rbDissatisfied.isChecked()) {
            return rbDissatisfied.getText().toString();
        }
        return "";
    }


    //改变选项
    private void changeOptions() {
        if (rbSatisfied.isChecked()) {
            cb1.setText(optionsSatisfied[0]);
            cb2.setText(optionsSatisfied[1]);
            cb3.setText(optionsSatisfied[2]);
            cb4.setText(optionsSatisfied[3]);
            cb5.setText(optionsSatisfied[4]);
            cb4.setVisibility(View.VISIBLE);
            cb5.setVisibility(View.VISIBLE);
        }
        if (rbGeneral.isChecked()) {
            cb1.setText(optionsGeneral[0]);
            cb2.setText(optionsGeneral[1]);
            cb3.setText(optionsGeneral[2]);
            cb4.setVisibility(View.GONE);
            cb5.setVisibility(View.GONE);
        }
        if (rbDissatisfied.isChecked()) {
            cb1.setText(optionsDissatisfied[0]);
            cb2.setText(optionsDissatisfied[1]);
            cb3.setText(optionsDissatisfied[2]);
            cb4.setText(optionsDissatisfied[3]);
            cb5.setText(optionsDissatisfied[4]);
            cb4.setVisibility(View.VISIBLE);
            cb5.setVisibility(View.VISIBLE);
        }
        clearCheckState();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.rb_satisfied://满意
                score = "3";
                changeOptions();
                break;
            case R.id.rb_general://一般
                score = "2";
                changeOptions();
                break;
            case R.id.rb_dissatisfied://不满意
                score = "1";
                changeOptions();
                break;
            case R.id.bt_assess_commit://提交
                commitData();
                break;
        }
    }

    //将状态设置为未选中
    private void clearCheckState() {
        cb1.setChecked(false);
        cb2.setChecked(false);
        cb3.setChecked(false);
        cb4.setChecked(false);
        cb5.setChecked(false);
    }
}
